package com.zjj.wangluo.lianxi.tcp02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 *
 * 需求2：服务端接收多条消息，客户端发送多条消息
 */
public class TCPServer {

    public static void main(String[] args) {
        System.out.println("---------服务器端----------");
        ServerSocket ss = null;
        Socket socket = null;
        BufferedReader br = null;
        try {
            //1.创建服务器
            ss = new ServerSocket(8888);
            //2.接收客户端
            socket = ss.accept();
            System.out.println("客户端已成功接入~");
            //3.读消息
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //循环读消息
            String str = null;
            while((str=br.readLine()) != null){
                System.out.println("读："+str);
                if (str.equals("886")){
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
